package uk.ac.open.kmi.dyniqx.wrappers;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;

import uk.ac.open.kmi.dyniqx.wrappers.TermWrapper;

public class TermFrequencyWrapper {

	public Logger logger = Logger.getLogger("uk.ac.open.kmi.dyniqx.wrappers.TermWrapper");
	public List<TermWrapper> al = new ArrayList<TermWrapper>(); 
	
	
	public TermFrequencyWrapper() {
		this.logger.debug("TermFrequencyWrapper instance created.");
	}
	
	public TermFrequencyWrapper(Hashtable<String, Integer> ht) {
	    for (Enumeration<String> e = ht.keys(); e.hasMoreElements();) {
	    	String key = (String) e.nextElement();
		    Integer value = (Integer) ht.get(key);
		    this.al.add(new TermWrapper(key, value));
		}
	}
	
	@SuppressWarnings("unchecked")
	public void manageList() {
		Collections.sort(this.al, TermWrapper.FreqComparator);
		Collections.reverse(this.al);
		
		int length = this.al.size();
		if (length > 50) length = 50;
		this.al = this.al.subList(0, length);
		
		Collections.sort(this.al, TermWrapper.WordComparator);
	}
	
	public String dumpString() {
		String output = "";
		
		Iterator<TermWrapper> it = this.al.iterator();
		TermWrapper current;
		
	    while (it.hasNext()) {
	        current = it.next();
	        long freq = Math.round(Math.sqrt(current.getFreq()));
	        if (freq > 10) freq = 10;
	    	output += "'" + current.getWord() + "': " + freq + ", ";    
	    }
		
	    output = output.substring(0, output.length()-2);
	    output = "{" + output + "}";
	    
		return output;
	}
}
